﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class YJLR_YJLRAdd : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("8"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      //if (CWSD.GetSDZT())
      //{
      //  Server.Transfer("~\\sd.aspx");
      //}

      dateJG.Text = DateTime.Now.ToString("yyyy-MM-dd");
      string sID = CommonUse.GetID();
      if (!string.IsNullOrEmpty(sID))
      {
        InitData(sID);
        ASPxHyperLink1.NavigateUrl = "YJLRList.aspx";
      }
      else
      {
        ASPxHyperLink1.NavigateUrl = "YJLRKHList.aspx";
      }
      InitKHXX(sID);
    }
  }

  protected void InitKHXX(string sID)
  {
    if (string.IsNullOrEmpty(sID))
    {
      lbKHID.Text = CommonUse.GetID("khid");
    }
    else
    {
      lbKHID.Text = YJLR.GetSKHID(sID);
    }
    lbKHMC.Text = KHXX.GetKHMC(lbKHID.Text);
  }

  protected void InitData(string sID)
  {
    SqlDataReader dr = YJLR.GetYJLR(sID);
    if (dr.Read())
    {
      txtName.Text = dr["yj"].ToString();
      hlName.Value = txtName.Text;
      dateJG.Text = dr["rq"].ToString();
      hlJG.Value = dateJG.Text;
      txtBZ.Text = dr["yjbz"].ToString();
      hlBZ.Value = txtBZ.Text;
      //sKHID = dr["khid"].ToString();
      //dateJG.Enabled = false;
    }
    dr.Close();
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    if (string.IsNullOrEmpty(CommonUse.GetID()))
      Insert();
    else
      Update();
  }

  //插入
  protected void Insert()
  {
    //if (ExistID())
    //{
    //  lbMessage.Text = "此日期已存在押金";
    //  return;
    //}
    string sID = CommonUse.GetID("sjlr_yjlr", "yjid");
    Save(sID);
  }

  protected void Save(string sID)
  {
    //if (string.IsNullOrEmpty(dateJG.Text))
    //{
    //  lbMessage.Text = "抄表时间不能为空";
    //  return;
    //}
    //if (!CommonUse.IsDouble(txtName))
    //{
    //  lbMessage.Text = "本月表数必须为整数";
    //  return;
    //}
    if (YJLR.ExistHQID(lbKHID.Text, dateJG.Text))
    {
      lbMessage.Text = "本次日期之后已存在数据，不能修改";
      return;
    }
    //string lj = YJLR.GetYJ(lbKHID.Text);
    List<CommandInfo> cis = new List<CommandInfo>();
    cis.Add(CommonUse.DeleteCI("sjlr_yjlr", "yjid", sID));

    ArrayList arrValue = new ArrayList();
    arrValue.Add(sID);
    arrValue.Add(SysManage.Get_User);
    arrValue.Add(lbKHID.Text);
    arrValue.Add(dateJG.Text);
    arrValue.Add(txtName.Text);
    arrValue.Add(txtBZ.Text);
    arrValue.Add(DBNull.Value);
    ArrayList arrLengh = new ArrayList();
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("200");
    arrLengh.Add("20");
    cis.Add(CommonUse.InsertCI("sjlr_yjlr", arrValue, arrLengh));

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      string sql = "update sjlr_yjlr set ljje =(select isnull(sum(yj),0) yj from SJLR_yjlr where khid=" + lbKHID.Text
        + ") where yjid=" + sID;
      if (DbHelperSQL.ExecuteSql(sql) > 0)
        lbMessage.Text = lbKHMC.Text + "保存成功";
      if (CommonUse.GetID() != "")
      {
        string nr = "";
        if (txtName.Text != hlName.Value)
          nr += "金额由" + hlName.Value + "改为" + txtName.Text + "  ";
        if (dateJG.Text != hlJG.Value)
          nr += "日期由" + hlJG.Value + "改为" + dateJG.Text + "  ";
        if (txtBZ.Text != hlBZ.Value)
          nr += "备注由" + txtBZ.Text + "改为" + hlBZ.Value + "  ";
        if (nr != "")
          rzcz.InsertRZ("修改履约保证金:客户编码为" + lbKHID.Text + ",日期为" + dateJG.Text + nr);
      }
    }

  }

  //修改
  protected void Update()
  {
    Save(CommonUse.GetID());
  }
}
